﻿using EmptoolWebApp.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace EmptoolWebApp.Webservices
{
    /// <summary>
    /// Summary description for SlackBotTestWebService
    /// </summary>
    [WebService(Namespace = "http://emptool.com/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [System.Web.Script.Services.ScriptService]
    public class SlackBotTestWebService : BaseWebService
    {

        [WebMethod]
        [System.Web.Script.Services.ScriptMethod(UseHttpGet = false, 
            ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)]
        public SlackBotResponse HelloWorld()
        {
            log4net.ILog log = log4net.LogManager.GetLogger("EmpToolWebsiteLog");
            log.Error("REACHED SlackBotTestWebService");

            System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();
            SlackBotResponse sbr = new SlackBotResponse();
            sbr.text = "SlackBotTestWebService YAY!!!!";


            //this.Context.Response.ContentType = "application/json; charset=utf-8";

            //return js.Serialize(sbr);
            return sbr;

            // ZX: ASMX will only return response in JSON, if (and only if) 
            // the request HTTP header "Content-Type" is "application/json"
            // Without that specific header value, it will almost always response in XML
            // This is an issue when writing Slack "Outgoing WebHooks".
            // This is because when Slack make the post call to the url specified, it does not add this header.
            // To overcome this issue, we:
            // 1. Clear Response object
            // 2. Set response Content-Type to be "application/json; charset=utf-8";
            //    (In the context of Slack response, this a nice-to-have but is really optional
            // 3. Write out the serialized result

            //HttpContext.Current.Response.Clear();
            //HttpContext.Current.Response.ContentType = "application/json; charset=utf-8";
            //HttpContext.Current.Response.Write(js.Serialize(sbr));
        }
    }
}
